// import store from '@/store'
import axios from 'axios'
import qs from 'qs'
import { Dialog, Toast } from 'vant'
import Vue from 'vue'
import router from '../router/index'
// create an axios instance
// const baseURL = process.env.VUE_APP_BASE_API
const service = axios.create({
  // baseURL: "http://10.20.32.70:48010", // 正式库
  baseURL: "http://10.20.32.33:28010", // 测试库 
  // baseURL: "http://172.19.0.24:28010",  // 本地
  // baseURL: "http://172.30.0.79:28010",  // 本地
  timeout: 300000 // request timeout ：： =>  超时时间 5分钟
})

// Vue.prototype.$url = 'http://192.168.1.56:88/' // 图片访问地址
Vue.prototype.$axios = axios
Vue.prototype.$qs = qs
/**  axios 请求前添加请求头  */
service.interceptors.request.use(
  config => {
    config.headers['Authorization'] = 'Basic ' + require('buffer').Buffer('mes' + ':' + 'mes').toString('base64')
    const token = localStorage.getItem('EMEStoken')
    if (token) {
      // 请求头添加token
      config.headers['Dobo-Auth'] = 'bearer ' + token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 请求服务返回值 拦截器
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response
    if (res.data.code === 200 || (!res.data.code && res.status === 200) || res.data.code === 400) {
      return res
    }
    // else if (res.data.code === 10010) {
    //   window.localStorage.removeItem('EMEStoken')
    //   Toast.fail(res.data.msg + '请再点击一次登录！' || '请求错误,请联系管理员！')
    // } 
    else {
      Toast.fail(res.data.msg || '请求错误,请联系管理员！')
    }
  },
  error => {
    if (error.response) {
      if (error.response.status === 403 || error.response.status === 401 || error.response.data.code === 403 || error.response.data.code === 401) {
        Vue.prototype.$dialog
          .alert({
            title: '提示',
            message: '用户信息失效,请重新登陆!'
          })
          .then(() => {
            router.push('/login')
          })
      } else {
        Dialog.alert({
          title: '提示',
          message: error.response.data.msg || '请求错误,请联系管理员！',
          theme: 'round-button'
        })
        // 返回 response 里的错误信息
        return Promise.reject(error.response.data)
      }
    }
    return Promise.reject(error)
  }
)
//  导出  service
export default service
